import {
  Body,
  Controller,
  HttpCode,
  HttpStatus,
  Logger,
  Post,
  UseGuards,
} from '@nestjs/common';
import { ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger';
// @app/common
import {
  IsPublic,
  IP,
  SwaggerBaseApiResponse,
  BaseApiErrorResponse,
  IRequestUser,
} from '@app/common';
import { LoginInputDto, LoginOutputDto } from './dtos';
import { AuthService } from './auth.service';
import { LocalGuard } from './guards/local.guard';

@ApiTags('auth')
@Controller('auth')
export class AuthController {
  // logger
  readonly logger = new Logger(AuthController.name);

  constructor(private readonly authService: AuthService) {}

  @Post('login')
  @IsPublic()
  @UseGuards(LocalGuard)
  @ApiOperation({
    summary: '系统登录接口',
  })
  @ApiResponse({
    status: HttpStatus.OK,
    type: SwaggerBaseApiResponse(LoginOutputDto),
  })
  @ApiResponse({
    status: HttpStatus.BAD_REQUEST,
    type: BaseApiErrorResponse,
  })
  @HttpCode(HttpStatus.OK)
  async login(
    @IRequestUser user: any,
    @Body() _: LoginInputDto,
    @IP() ip: string,
  ): Promise<any> {
    return await this.authService.login(user, ip);
  }
}
